Skip to content

Method: createEDoubleObjectFromString(EDataType, String)

1: /*******************************************************************************
2: * Copyright (c) 2011-2014 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * jfaltermeier - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.edapt;
15:
16: import java.math.BigDecimal;
17: import java.math.BigInteger;
18: import java.text.ParseException;
19: import java.util.Date;
20:
21: import org.eclipse.emf.common.util.WrappedException;
22: import org.eclipse.emf.ecore.EDataType;
23: import org.eclipse.emf.ecore.impl.EFactoryImpl;
24:
25: /**
26: * {@link org.eclipse.emf.ecore.EFactory EFactory} implementation for creating objects for the custom data types of the
27: * {@link org.eclipse.emf.ecore.EcoreFactory EcoreFactory}.
28: *
29: * Since edapt uses dynamic EMF the {@link org.eclipse.emf.ecore.EcoreFactory EcoreFactory} is not used when loading a
30: * model, we need a custom implementation. Moreover the switch should be based on the data type names, since the ids
31: * might differ as well.
32: *
33: * @author jfaltermeier
34: *
35: */
36: public class DynamicEcoreFactory extends EFactoryImpl {
37:
38:         private static final String E_BIG_DECIMAL = "EBigDecimal";//$NON-NLS-1$
39:         private static final String E_BIG_INTEGER = "EBigInteger";//$NON-NLS-1$
40:         private static final String E_BOOLEAN = "EBoolean";//$NON-NLS-1$
41:         private static final String E_BOOLEAN_OBJECT = "EBooleanObject";//$NON-NLS-1$
42:         private static final String E_BYTE = "EByte";//$NON-NLS-1$
43:         private static final String E_BYTE_ARRAY = "EByteArray";//$NON-NLS-1$
44:         private static final String E_BYTE_OBJECT = "EByteObject";//$NON-NLS-1$
45:         private static final String E_CHAR = "EChar";//$NON-NLS-1$
46:         private static final String E_CHARACTER_OBJECT = "ECharacterObject";//$NON-NLS-1$
47:         private static final String E_DATE = "EDate";//$NON-NLS-1$
48:         private static final String E_DOUBLE = "EDouble";//$NON-NLS-1$
49:         private static final String E_DOUBLE_OBJECT = "EDoubleObject";//$NON-NLS-1$
50:         private static final String E_FLOAT = "EFloat";//$NON-NLS-1$
51:         private static final String E_FLOAT_OBJECT = "EFloatObject";//$NON-NLS-1$
52:         private static final String E_INT = "EInt";//$NON-NLS-1$
53:         private static final String E_INTEGER_OBJECT = "EIntegerObject";//$NON-NLS-1$
54:         private static final String E_JAVA_CLASS = "EJavaClass";//$NON-NLS-1$
55:         private static final String E_JAVA_OBJECT = "EJavaObject";//$NON-NLS-1$
56:         private static final String E_LONG = "ELong";//$NON-NLS-1$
57:         private static final String E_LONG_OBJECT = "ELongObject";//$NON-NLS-1$
58:         private static final String E_SHORT = "EShort";//$NON-NLS-1$
59:         private static final String E_SHORT_OBJECT = "EShortObject";//$NON-NLS-1$
60:         private static final String E_STRING = "EString";//$NON-NLS-1$
61:
62:         /**
63:          *
64:          * {@inheritDoc}
65:          *
66:          * @see org.eclipse.emf.ecore.impl.EFactoryImpl#createFromString(org.eclipse.emf.ecore.EDataType, java.lang.String)
67:          */
68:         // BEGIN COMPLEX CODE
69:         @Override
70:         public Object createFromString(EDataType eDataType, String initialValue) {
71:                 final String id = eDataType.getName();
72:
73:                 if (E_BIG_DECIMAL.equals(id)) {
74:                         return createEBigDecimalFromString(eDataType, initialValue);
75:                 } else if (E_BIG_INTEGER.equals(id)) {
76:                         return createEBigIntegerFromString(eDataType, initialValue);
77:                 } else if (E_BOOLEAN.equals(id)) {
78:                         return createEBooleanFromString(eDataType, initialValue);
79:                 } else if (E_BOOLEAN_OBJECT.equals(id)) {
80:                         return createEBooleanObjectFromString(eDataType, initialValue);
81:                 } else if (E_BYTE.equals(id)) {
82:                         return createEByteFromString(eDataType, initialValue);
83:                 } else if (E_BYTE_ARRAY.equals(id)) {
84:                         return createEByteArrayFromString(eDataType, initialValue);
85:                 } else if (E_BYTE_OBJECT.equals(id)) {
86:                         return createEByteObjectFromString(eDataType, initialValue);
87:                 } else if (E_CHAR.equals(id)) {
88:                         return createECharFromString(eDataType, initialValue);
89:                 } else if (E_CHARACTER_OBJECT.equals(id)) {
90:                         return createECharacterObjectFromString(eDataType, initialValue);
91:                 } else if (E_DATE.equals(id)) {
92:                         return createEDateFromString(eDataType, initialValue);
93:                 } else if (E_DOUBLE.equals(id)) {
94:                         return createEDoubleFromString(eDataType, initialValue);
95:                 } else if (E_DOUBLE_OBJECT.equals(id)) {
96:                         return createEDoubleObjectFromString(eDataType, initialValue);
97:                 } else if (E_FLOAT.equals(id)) {
98:                         return createEFloatFromString(eDataType, initialValue);
99:                 } else if (E_FLOAT_OBJECT.equals(id)) {
100:                         return createEFloatObjectFromString(eDataType, initialValue);
101:                 } else if (E_INT.equals(id)) {
102:                         return createEIntFromString(eDataType, initialValue);
103:                 } else if (E_INTEGER_OBJECT.equals(id)) {
104:                         return createEIntegerObjectFromString(eDataType, initialValue);
105:                 } else if (E_JAVA_CLASS.equals(id)) {
106:                         return createEJavaClassFromString(eDataType, initialValue);
107:                 } else if (E_JAVA_OBJECT.equals(id)) {
108:                         return createEJavaObjectFromString(eDataType, initialValue);
109:                 } else if (E_LONG.equals(id)) {
110:                         return createELongFromString(eDataType, initialValue);
111:                 } else if (E_LONG_OBJECT.equals(id)) {
112:                         return createELongObjectFromString(eDataType, initialValue);
113:                 } else if (E_SHORT.equals(id)) {
114:                         return createEShortFromString(eDataType, initialValue);
115:                 } else if (E_SHORT_OBJECT.equals(id)) {
116:                         return createEShortObjectFromString(eDataType, initialValue);
117:                 } else if (E_STRING.equals(id)) {
118:                         return createEStringFromString(eDataType, initialValue);
119:                 }
120:                 return super.createFromString(eDataType, initialValue);
121:         }
122:
123:         // END COMPLEX CODE
124:
125:         private Boolean booleanValueOf(String initialValue) {
126:                 if ("true".equalsIgnoreCase(initialValue)) { //$NON-NLS-1$
127:                         return Boolean.TRUE;
128:                 } else if ("false".equalsIgnoreCase(initialValue)) { //$NON-NLS-1$
129:                         return Boolean.FALSE;
130:                 } else {
131:                         throw new IllegalArgumentException("Expecting true or false"); //$NON-NLS-1$
132:                 }
133:         }
134:
135:         private Boolean createEBooleanObjectFromString(EDataType metaObject,
136:                 String initialValue) {
137:                 return initialValue == null ? null : booleanValueOf(initialValue);
138:         }
139:
140:         private Character createECharacterObjectFromString(EDataType metaObject,
141:                 String initialValue) {
142:                 if (initialValue == null) {
143:                         return null;
144:                 }
145:
146:                 char charValue = 0;
147:                 try {
148:                         charValue = (char) Integer.parseInt(initialValue);
149:                 } catch (final NumberFormatException e) {
150:                         final char[] carray = initialValue.toCharArray();
151:                         charValue = carray[0];
152:                 }
153:                 return charValue;
154:         }
155:
156:         private Date createEDateFromString(EDataType eDataType, String initialValue) {
157:                 if (initialValue == null) {
158:                         return null;
159:                 }
160:
161:                 Exception exception = null;
162:                 for (int i = 0; i < EDATE_FORMATS.length; ++i) {
163:                         try {
164:                                 return EDATE_FORMATS[i].parse(initialValue);
165:                         } catch (final ParseException parseException) {
166:                                 exception = parseException;
167:                         }
168:                 }
169:                 throw new WrappedException(exception);
170:         }
171:
172:         private Double createEDoubleObjectFromString(EDataType metaObject,
173:                 String initialValue) {
174:•                return initialValue == null ? null : Double.valueOf(initialValue);
175:         }
176:
177:         private Float createEFloatObjectFromString(EDataType metaObject,
178:                 String initialValue) {
179:                 return initialValue == null ? null : Float.valueOf(initialValue);
180:         }
181:
182:         private Integer createEIntegerObjectFromString(EDataType metaObject,
183:                 String initialValue) {
184:                 return initialValue == null ? null : Integer.valueOf(initialValue);
185:         }
186:
187:         private BigDecimal createEBigDecimalFromString(EDataType eDataType,
188:                 String initialValue) {
189:                 return initialValue == null ? null : new BigDecimal(initialValue);
190:         }
191:
192:         private BigInteger createEBigIntegerFromString(EDataType eDataType,
193:                 String initialValue) {
194:                 return initialValue == null ? null : new BigInteger(initialValue);
195:         }
196:
197:         private String createEStringFromString(EDataType metaObject,
198:                 String initialValue) {
199:                 return initialValue;
200:         }
201:
202:         private Integer createEIntFromString(EDataType metaObject,
203:                 String initialValue) {
204:                 return initialValue == null ? null : Integer.valueOf(initialValue);
205:         }
206:
207:         private Boolean createEBooleanFromString(EDataType metaObject,
208:                 String initialValue) {
209:                 return initialValue == null ? null : booleanValueOf(initialValue);
210:         }
211:
212:         private Byte createEByteObjectFromString(EDataType metaObject,
213:                 String initialValue) {
214:                 return initialValue == null ? null : Byte.valueOf(initialValue);
215:         }
216:
217:         private Float createEFloatFromString(EDataType metaObject,
218:                 String initialValue) {
219:                 return initialValue == null ? null : Float.valueOf(initialValue);
220:         }
221:
222:         private Character createECharFromString(EDataType metaObject,
223:                 String initialValue) {
224:                 if (initialValue == null) {
225:                         return null;
226:                 }
227:                 char charValue = 0;
228:                 try {
229:                         charValue = (char) Integer.parseInt(initialValue);
230:                 } catch (final NumberFormatException e) {
231:                         final char[] carray = initialValue.toCharArray();
232:                         charValue = carray[0];
233:                 }
234:                 return charValue;
235:         }
236:
237:         private Long createELongFromString(EDataType metaObject, String initialValue) {
238:                 return initialValue == null ? null : Long.valueOf(initialValue);
239:         }
240:
241:         private Double createEDoubleFromString(EDataType metaObject,
242:                 String initialValue) {
243:                 return initialValue == null ? null : Double.valueOf(initialValue);
244:         }
245:
246:         private Byte createEByteFromString(EDataType metaObject, String initialValue) {
247:                 return initialValue == null ? null : Byte.valueOf(initialValue);
248:         }
249:
250:         private Short createEShortFromString(EDataType metaObject,
251:                 String initialValue) {
252:                 return initialValue == null ? null : Short.valueOf(initialValue);
253:         }
254:
255:         private Class<?> createEJavaClassFromString(EDataType metaObject,
256:                 String initialValue) {
257:                 try {
258:                         if (initialValue == null) {
259:                                 return null;
260:                         } else if ("boolean".equals(initialValue)) { //$NON-NLS-1$
261:                                 return boolean.class;
262:                         } else if ("byte".equals(initialValue)) { //$NON-NLS-1$
263:                                 return byte.class;
264:                         } else if ("char".equals(initialValue)) { //$NON-NLS-1$
265:                                 return char.class;
266:                         } else if ("double".equals(initialValue)) { //$NON-NLS-1$
267:                                 return double.class;
268:                         } else if ("float".equals(initialValue)) { //$NON-NLS-1$
269:                                 return float.class;
270:                         } else if ("int".equals(initialValue)) { //$NON-NLS-1$
271:                                 return int.class;
272:                         } else if ("long".equals(initialValue)) { //$NON-NLS-1$
273:                                 return long.class;
274:                         } else if ("short".equals(initialValue)) { //$NON-NLS-1$
275:                                 return short.class;
276:                         } else {
277:                                 return Class.forName(initialValue);
278:                         }
279:                 } catch (final ClassNotFoundException e) {
280:                         throw new WrappedException(e);
281:                 }
282:         }
283:
284:         private Object createEJavaObjectFromString(EDataType eDataType,
285:                 String initialValue) {
286:                 return createFromString(initialValue);
287:         }
288:
289:         private Long createELongObjectFromString(EDataType metaObject,
290:                 String initialValue) {
291:                 return initialValue == null ? null : Long.valueOf(initialValue);
292:         }
293:
294:         private Short createEShortObjectFromString(EDataType metaObject,
295:                 String initialValue) {
296:                 return initialValue == null ? null : Short.valueOf(initialValue);
297:         }
298:
299:         private byte[] createEByteArrayFromString(EDataType eDataType,
300:                 String initialValue) {
301:                 return hexStringToByteArray(initialValue);
302:         }
303:
304:         private byte[] hexStringToByteArray(String initialValue) {
305:                 if (initialValue == null) {
306:                         return null;
307:                 }
308:
309:                 final int size = initialValue.length();
310:                 int limit = (size + 1) / 2;
311:                 final byte[] result = new byte[limit];
312:                 if (size % 2 != 0) {
313:                         result[--limit] = hexCharToByte(initialValue.charAt(size - 1));
314:                 }
315:
316:                 for (int i = 0, j = 0; i < limit; ++i) {
317:                         final byte high = hexCharToByte(initialValue.charAt(j++));
318:                         final byte low = hexCharToByte(initialValue.charAt(j++));
319:                         result[i] = (byte) (high << 4 | low);
320:                 }
321:                 return result;
322:         }
323:
324:         // BEGIN COMPLEX CODE
325:         private byte hexCharToByte(char character) {
326:                 switch (character) {
327:                 case '0':
328:                 case '1':
329:                 case '2':
330:                 case '3':
331:                 case '4':
332:                 case '5':
333:                 case '6':
334:                 case '7':
335:                 case '8':
336:                 case '9': {
337:                         return (byte) (character - '0');
338:                 }
339:                 case 'a':
340:                 case 'b':
341:                 case 'c':
342:                 case 'd':
343:                 case 'e':
344:                 case 'f': {
345:                         return (byte) (character - 'a' + 10);
346:                 }
347:                 case 'A':
348:                 case 'B':
349:                 case 'C':
350:                 case 'D':
351:                 case 'E':
352:                 case 'F': {
353:                         return (byte) (character - 'A' + 10);
354:                 }
355:                 default: {
356:                         throw new NumberFormatException("Invalid hexadecimal"); //$NON-NLS-1$
357:                 }
358:                 }
359:         }
360:
361:         // END COMPLEX CODE
362:
363:         // BEGIN COMPLEX CODE
364:         @Override
365:         public String convertToString(EDataType eDataType, Object instanceValue) {
366:
367:                 final String id = eDataType.getName();
368:
369:                 if (E_BIG_DECIMAL.equals(id)) {
370:                         return convertEBigDecimalToString(eDataType, instanceValue);
371:                 } else if (E_BIG_INTEGER.equals(id)) {
372:                         return convertEBigIntegerToString(eDataType, instanceValue);
373:                 } else if (E_BOOLEAN.equals(id)) {
374:                         return convertEBooleanToString(eDataType, instanceValue);
375:                 } else if (E_BOOLEAN_OBJECT.equals(id)) {
376:                         return convertEBooleanObjectToString(eDataType, instanceValue);
377:                 } else if (E_BYTE.equals(id)) {
378:                         return convertEByteToString(eDataType, instanceValue);
379:                 } else if (E_BYTE_ARRAY.equals(id)) {
380:                         return convertEByteArrayToString(eDataType, instanceValue);
381:                 } else if (E_BYTE_OBJECT.equals(id)) {
382:                         return convertEByteObjectToString(eDataType, instanceValue);
383:                 } else if (E_CHAR.equals(id)) {
384:                         return convertECharToString(eDataType, instanceValue);
385:                 } else if (E_CHARACTER_OBJECT.equals(id)) {
386:                         return convertECharacterObjectToString(eDataType, instanceValue);
387:                 } else if (E_DATE.equals(id)) {
388:                         return convertEDateToString(eDataType, instanceValue);
389:                 } else if (E_DOUBLE.equals(id)) {
390:                         return convertEDoubleToString(eDataType, instanceValue);
391:                 } else if (E_DOUBLE_OBJECT.equals(id)) {
392:                         return convertEDoubleObjectToString(eDataType, instanceValue);
393:                 } else if (E_FLOAT.equals(id)) {
394:                         return convertEFloatToString(eDataType, instanceValue);
395:                 } else if (E_FLOAT_OBJECT.equals(id)) {
396:                         return convertEFloatObjectToString(eDataType, instanceValue);
397:                 } else if (E_INT.equals(id)) {
398:                         return convertEIntToString(eDataType, instanceValue);
399:                 } else if (E_INTEGER_OBJECT.equals(id)) {
400:                         return convertEIntegerObjectToString(eDataType, instanceValue);
401:                 } else if (E_JAVA_CLASS.equals(id)) {
402:                         return convertEJavaClassToString(eDataType, instanceValue);
403:                 } else if (E_JAVA_OBJECT.equals(id)) {
404:                         return convertEJavaObjectToString(eDataType, instanceValue);
405:                 } else if (E_LONG.equals(id)) {
406:                         return convertELongToString(eDataType, instanceValue);
407:                 } else if (E_LONG_OBJECT.equals(id)) {
408:                         return convertELongObjectToString(eDataType, instanceValue);
409:                 } else if (E_SHORT.equals(id)) {
410:                         return convertEShortToString(eDataType, instanceValue);
411:                 } else if (E_SHORT_OBJECT.equals(id)) {
412:                         return convertEShortObjectToString(eDataType, instanceValue);
413:                 } else if (E_STRING.equals(id)) {
414:                         return convertEStringToString(eDataType, instanceValue);
415:                 }
416:                 return super.convertToString(eDataType, instanceValue);
417:         }
418:
419:         // END COMPLEX CODE
420:
421:         private String convertEBooleanObjectToString(EDataType metaObject, Object instanceValue) {
422:                 return instanceValue == null ? null : instanceValue.toString();
423:         }
424:
425:         private String convertECharacterObjectToString(EDataType metaObject, Object instanceValue) {
426:                 if (instanceValue instanceof Character) {
427:                         return Integer.toString((Character) instanceValue);
428:                 }
429:
430:                 return instanceValue == null ? null : instanceValue.toString();
431:         }
432:
433:         private String convertEDateToString(EDataType eDataType, Object instanceValue) {
434:                 if (instanceValue == null) {
435:                         return null;
436:                 }
437:
438:                 return EDATE_FORMATS[0].format((Date) instanceValue);
439:
440:         }
441:
442:         private String convertEDoubleObjectToString(EDataType metaObject, Object instanceValue) {
443:                 return instanceValue == null ? null : instanceValue.toString();
444:         }
445:
446:         private String convertEFloatObjectToString(EDataType metaObject, Object instanceValue) {
447:                 return instanceValue == null ? null : instanceValue.toString();
448:         }
449:
450:         private String convertEIntegerObjectToString(EDataType metaObject, Object instanceValue) {
451:                 return instanceValue == null ? null : instanceValue.toString();
452:         }
453:
454:         private String convertEBigDecimalToString(EDataType eDataType, Object instanceValue) {
455:                 return instanceValue == null ? null : instanceValue.toString();
456:         }
457:
458:         private String convertEBigIntegerToString(EDataType eDataType, Object instanceValue) {
459:                 return instanceValue == null ? null : instanceValue.toString();
460:         }
461:
462:         private String convertEStringToString(EDataType metaObject, Object instanceValue) {
463:                 return (String) instanceValue;
464:         }
465:
466:         private String convertEIntToString(EDataType metaObject, Object instanceValue) {
467:                 return instanceValue == null ? null : instanceValue.toString();
468:         }
469:
470:         private String convertEBooleanToString(EDataType metaObject, Object instanceValue) {
471:                 return instanceValue == null ? null : instanceValue.toString();
472:         }
473:
474:         private String convertEByteObjectToString(EDataType metaObject, Object instanceValue) {
475:                 return instanceValue == null ? null : instanceValue.toString();
476:         }
477:
478:         private String convertEFloatToString(EDataType metaObject, Object instanceValue) {
479:                 return instanceValue == null ? null : instanceValue.toString();
480:         }
481:
482:         private String convertECharToString(EDataType metaObject, Object instanceValue) {
483:                 if (instanceValue instanceof Character) {
484:                         return Integer.toString((Character) instanceValue);
485:                 }
486:
487:                 return instanceValue == null ? null : instanceValue.toString();
488:         }
489:
490:         private String convertELongToString(EDataType metaObject, Object instanceValue) {
491:                 return instanceValue == null ? null : instanceValue.toString();
492:         }
493:
494:         private String convertEDoubleToString(EDataType metaObject, Object instanceValue) {
495:                 return instanceValue == null ? null : instanceValue.toString();
496:         }
497:
498:         private String convertEByteToString(EDataType metaObject, Object instanceValue) {
499:                 return instanceValue == null ? null : instanceValue.toString();
500:         }
501:
502:         private String convertEByteArrayToString(EDataType eDataType, Object instanceValue) {
503:                 if (instanceValue == null) {
504:                         return null;
505:                 }
506:                 final byte[] bytes = (byte[]) instanceValue;
507:                 return convertBytesToHexString(bytes, bytes.length);
508:
509:         }
510:
511:         private String convertEShortToString(EDataType metaObject, Object instanceValue) {
512:                 return instanceValue == null ? null : instanceValue.toString();
513:         }
514:
515:         private String convertEJavaClassToString(EDataType metaObject, Object instanceValue) {
516:                 return instanceValue == null ? "" : ((Class<?>) instanceValue).getName(); //$NON-NLS-1$
517:         }
518:
519:         private String convertEJavaObjectToString(EDataType eDataType, Object instanceValue) {
520:                 return convertToString(instanceValue);
521:         }
522:
523:         private String convertELongObjectToString(EDataType metaObject, Object instanceValue) {
524:                 return instanceValue == null ? null : instanceValue.toString();
525:         }
526:
527:         private String convertEShortObjectToString(EDataType metaObject, Object instanceValue) {
528:                 return instanceValue == null ? null : instanceValue.toString();
529:         }
530:
531:         private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
532:                 'E', 'F' };
533:
534:         private String convertBytesToHexString(byte[] bytes, int count) {
535:                 if (bytes == null) {
536:                         return null;
537:                 }
538:                 final char[] result = new char[2 * count];
539:                 for (int i = 0, j = 0; i < count; ++i) {
540:                         final int high = bytes[i] >> 4 & 0xF;
541:                         final int low = bytes[i] & 0xF;
542:                         result[j++] = HEX_DIGITS[high];
543:                         result[j++] = HEX_DIGITS[low];
544:                 }
545:                 return new String(result);
546:
547:         }
548:
549: }